package com.hdu.controller;


import com.hdu.common.Result;
import com.hdu.common.ResultCode;
import com.hdu.exception.CustomException;
import com.hdu.pojo.User;
import com.hdu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    public Result<String> login(String username,String password){
//        System.out.println(username);
        if (username==null||password==null){
            throw new CustomException("用户密码值为空，发送未知错误！");
        }

        if(!userService.login(username,password)){
            throw new CustomException("登录失败,错误！");
        }

        return new Result<>(ResultCode.SUCCESS,"登录成功！");
    }

    @PostMapping("/register")
    public Result<String> register(String username,String password){
        if (username == null || password == null){
            throw new CustomException("用户密码值为空，发送未知错误！");
        }

        if (!userService.register(username,password)){
            throw new CustomException("注册失败");
        }

        return new Result<>(ResultCode.SUCCESS,"注册成功！");
    }

}
